﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using FiveStone.MyStack;
using FiveStone.Service;

namespace FiveStone
{
    public partial class Form1 : Form
    {        
        public Boards bd;
        private bool isMeFirst = true;

        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X < 680 && e.Y < 680)
            {
                int m = (int)(e.X / 42);
                int n = (int)(e.Y / 42);
                if (m < 0){ m = 0; }
                if (n < 0){ n = 0; }
                if (m > 14){ m = 14; }
                if (n > 14){ n = 14; }
                //label1.Text = "X：" + m.ToString() + "  Y：" + n.ToString();
                toolStripStatusLabel1.Text = "落子点：(X " + m.ToString() + " / Y " + n.ToString() + ")";
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (bd != null)
            {
                bd.PersonPut(e.X, e.Y);
                if(!bd.PlayerIsPC)
                    PutState(false);
            }
        }                

        private void 玩家先ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!玩家先ToolStripMenuItem.Checked)
            {
                historylist.Items.Clear();                               
                玩家先ToolStripMenuItem.Checked = true;
                电脑先ToolStripMenuItem.Checked = false;
                black.Text = User.MyName;
                white.Text = User.PlayerName;
                isMeFirst = true;
            }
        }

        private void 电脑先ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!电脑先ToolStripMenuItem.Checked)
            {
                historylist.Items.Clear();
                电脑先ToolStripMenuItem.Checked = true;
                玩家先ToolStripMenuItem.Checked = false;
                black.Text = User.PlayerName;
                white.Text = User.MyName;                
                isMeFirst = false;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 单机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartGame(true);
        }        

        /// <summary>
        /// 创建主机
        /// </summary>        
        private void 联机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowOnlineForm(true);
        }
        /// <summary>
        /// 加入游戏
        /// </summary>        
        private void JoinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowOnlineForm(false);
        }

        private void ShowOnlineForm(bool isMeServer)
        {
            black.Text = white.Text = String.Empty;
            Online onlineFrm = new Online(isMeServer);
            onlineFrm.ShowDialog();
            if (onlineFrm.DialogResult == DialogResult.OK)//创建主机
            {
                NetProcessor.mfrom = this;
                StartGame(false);
                this.Text = "五子棋          等待加入游戏...";
            }
            if (onlineFrm.DialogResult == DialogResult.Yes)//加入游戏
            {
                NetProcessor.mfrom = this;
                NetProcessor.SendJoin();
                StartGame(false);
                PutState(false);
            }
            if (onlineFrm.DialogResult != DialogResult.Cancel)
            {
                创建主机ToolStripMenuItem.Enabled = false;
                加入游戏JToolStripMenuItem.Enabled = false;
                联机ToolStripMenuItem.Enabled = false;
                JoinToolStripMenuItem.Enabled = false;
            }
        }

        /// <summary>
        /// 游戏开始准备
        /// </summary>
        /// <param name="isPc"></param>
        private void StartGame(bool isPc)
        {
            historylist.Items.Clear();
            Graphics g = this.CreateGraphics();
            bd = new Boards(g, new Game(),historylist);            
            bd.ClearBoards();
            bd.DrawBoard();
            bd.PlayerIsPC = isPc;//对方是否为电脑
            if (isPc)//对手是否电脑
            {
                black.Text = isMeFirst == true ? "玩家" : "计算机";
                white.Text = isMeFirst == false ? "玩家" : "计算机";
                black.ForeColor = Color.Black;
                white.ForeColor = Color.Black;
                btSendMsg.Enabled = false;
                this.Text = "五子棋";
                bd.Start(isMeFirst);//True  玩家先 我先 -- False 电脑先  对方先
            }            
        }

        /// <summary>
        /// 游戏正式开始
        /// </summary>
        /// <param name="isServer"></param>
        public void Start(bool isServer)
        {
            if (isServer)
            {
                black.Text = User.MyName;
                white.Text = User.PlayerName;
                bd.Start(User.isMeFirst);
                PutState(true);                
            }
            else
            {
                black.Text = User.PlayerName;
                white.Text = User.MyName;
                bd.Start(User.isMeFirst);
                PutState(false);
            }
            btSendMsg.Enabled = true;
            this.Text = "五子棋";
            bd.DrawString("游戏开始！");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            QuitGame();
        }

        private void QuitGame()
        {
            NetProcessor.StopService();
        }

        /// <summary>
        /// 对方下棋
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="name"></param>
        /// <param name="isSocketPut"></param>
        public void Put(int x, int y, string name, bool isSocketPut)
        {
            bd.PersonPut(x, y, name, isSocketPut);
            PutState(true);
        }

        /// <summary>
        /// 当前下棋状态
        /// </summary>
        /// <param name="isMePut"></param>
        public void PutState(bool isMePut)
        {            
            black.ForeColor = (isMePut == true ? (User.isMeFirst == true ? Color.Red : Color.Black) : (User.isMeFirst == true ? Color.Black : Color.Red));
            white.ForeColor = (isMePut == true ? (User.isMeFirst == true ? Color.Black : Color.Red) : (User.isMeFirst == true ? Color.Red : Color.Black));            
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (bd != null)
            {
                bd.DrawBoard();
            }
        }

        private void btSendMsg_Click(object sender, EventArgs e)
        {
            string strMsg = RichText(string.Format(" 说({0}):\r\n{1}\r\n", System.DateTime.Now.ToString("HH:mm:ss"), txbSendMsg.Text.Trim()));
            //txbMsg.Text += strMsg;
            rtxbMsg.Select(rtxbMsg.Text.Length, 0);
            rtxbMsg.SelectedRtf = strMsg;
            rtxbMsg.ScrollToCaret();

            txbSendMsg.Text = String.Empty;
            NetProcessor.SendChatMessage(strMsg);
        }

        public void ChatMsg(string strMsg)
        {
            rtxbMsg.Select(rtxbMsg.Text.Length, 0);
            rtxbMsg.SelectedRtf = strMsg;
            rtxbMsg.ScrollToCaret();
        }

        private string RichText(string str)
        {
            RichTextBox rtb = new RichTextBox();
            rtb.Text = User.MyName + str;
            rtb.Select(0, User.MyName.Length);
            rtb.SelectionColor = Color.Green;
            rtb.SelectAll();
            return rtb.SelectedRtf;
        }
        
    }
}
